/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.line.pipe.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.bizz.line.constant.enums.ParkLineModDictEnum;
import com.hopes.bizz.line.constant.enums.ParkLineSysDictEnum;
import com.hopes.bizz.line.pipe.entity.LineValveEntity;
import com.hopes.bizz.line.pipe.dto.LineValveExcelDTO;
import com.hopes.bizz.line.pipe.vo.LineValveExcelVO;
import com.hopes.bizz.line.pipe.mapper.LineValveMapper;
import com.hopes.bizz.line.pipe.service.LineValveService;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.SysDictEnum;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
 * 阀门
 *
 * @author pig
 * @date 2024-02-21 21:47:26
 */
@Service
public class LineValveServiceImpl extends ServiceImpl<LineValveMapper, LineValveEntity> implements LineValveService {

    @Override
    public ValidateImportBO<LineValveExcelDTO> validateImport(List<LineValveExcelDTO> excelDTOList) {
        ValidateImportBO<LineValveExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<LineValveExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        for (LineValveExcelDTO lineValveExcelDTO : excelDTOList) {
            final String validateRes = ValidatorUtils.importValidateEntity(lineValveExcelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(lineValveExcelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(lineValveExcelDTO.getLineNum(), new HashSet<>(){
                    private static final long serialVersionUID = 1544007667186208650L;
                    {
                        add(validateRes);
                    }
                }));
            }
        }

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public LineValveEntity importDtoToEntity(LineValveExcelDTO excelDto) {
        LineValveEntity entity= new LineValveEntity();
        BeanUtils.copyProperties(excelDto, entity);
        if (StrUtil.isNotBlank(excelDto.getIsUseStr())) {
            entity.setIsUse(DictResolver.getItemValueByLabel(SysDictEnum.yes_no_type, excelDto.getIsUseStr()));
        }
        if (StrUtil.isNotBlank(excelDto.getPipeIdStr())) {
            entity.setPipeId(DictResolver.getItemValueByLabel(ParkLineModDictEnum.line_pipe, excelDto.getPipeIdStr(), Long::valueOf));
        }
        if (StrUtil.isNotBlank(excelDto.getEffectStr())) {
            entity.setEffect(DictResolver.getItemValueByLabel(ParkLineSysDictEnum.line_valve_effect, excelDto.getEffectStr()));
        }
        return entity;
    }

    @Override
    public LineValveExcelVO exportEntityToVo(LineValveEntity entity) {
        LineValveExcelVO excelVO = new LineValveExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        excelVO.setLocationStr(StrUtil.isBlank(entity.getLocation()) ? "未定位" : "已定位");
        return excelVO;
    }
}
